package org.ns.summer.beans.anns;

import org.ns.summer.beans.BeanPostProcessor;
import org.ns.summer.core.AnnotationUtils;

import java.util.Collections;
import java.util.List;

public class ProcessorOrderUtils {

    public static void sort(List<BeanPostProcessor> beanPostProcessors) {
        Collections.sort(beanPostProcessors, ((o1, o2) -> {
            Integer ov1 = getOrderValue(o1);
            Integer ov2 = getOrderValue(o2);
            return ov2.compareTo(ov1);
        }));
    }

    private static Integer getOrderValue(BeanPostProcessor o) {
        int value = 0;
        if (AnnotationUtils.existAnnotation(o.getClass(), Order.class)) {
            Order order = (Order) AnnotationUtils.getAnnotation(o.getClass(), Order.class);
            value = order.value();
        }
        return value;
    }
}
